﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace WindowsGame4
{
    class Projectile_MoveState : State
    {
        private static Projectile_MoveState instance;

        private Projectile_MoveState()
        {
            base.Name = "MoveState";
        }

        public override void enter(AIObject aiObject)
        {
   
        }

        public static Projectile_MoveState getInstance()
        {
            if (instance == null)
                instance = new Projectile_MoveState();
            return instance;
        }

        public override void execute(AIObject aiObject)
        {
             // Check if is in the destine
            //Console.WriteLine("<Projectile_MoveState> execute");
            AIProjectile aiProjectile = (AIProjectile)aiObject;
            Projectile projectile = (Projectile)aiProjectile.Projectile;
            GameInfo gameInfo = GameInfo.getInstance();

            //System.Console.WriteLine("projectilemovestate");
            projectile.move();
 
            if (aiProjectile.Projectile.Movement == null)
            {
                // Delete object
                EventEmitter.getInstance().registerMessage(0, aiProjectile.Id, aiProjectile.Id, Message.MessageType.msg_destroy);
            }
            else
            {
                // Check colision

                int row = gameInfo.World.getNumRow(projectile.Position.Y);
                int column = gameInfo.World.getNumColumn(projectile.Position.X);

                ArrayList ignore = new ArrayList();
                ArrayList type = new ArrayList();
                type.Add("enemy");
                type.Add("player");
                type.Add("projectile");
                ignore.Add(projectile.IdEmissor);
                ignore.Add(projectile.Id);

                Object victim = gameInfo.checkColisionProjectile(projectile, type, ignore);
                if (victim != null)
                {
                    aiProjectile.Projectile.Movement = null;
                    projectile.IdVictim = victim.Id;
                    EventEmitter.getInstance().registerMessage(0, aiProjectile.Id, aiProjectile.Id, Message.MessageType.msg_destroy);
                }
                else
                {

                }

                /*
                GameInfo gameInfo = GameInfo.getInstance();
                int row = gameInfo.World.getNumRow(projectile.Position.Y);
                int column = gameInfo.World.getNumColumn(projectile.Position.X);
                Cell cell = gameInfo.World.get(row, column);
                if (cell.Obj != null && cell.Obj.Id != projectile.IdEmissor)
                {
              
                    // Colision detected
                    aiProjectile.Projectile.Movement = null;
                    EventEmitter.getInstance().registerMessage(0, aiProjectile.Id, aiProjectile.Id, Message.MessageType.msg_destroy);
                }*/

                /*
                int row = gameInfo.World.getNumRow(projectile.Position.Y);
                int column = gameInfo.World.getNumColumn(projectile.Position.X);

                ArrayList type = new ArrayList();
                 
                type.Add("projectile");
                type.Add("player");
                type.Add("enemy");
                ArrayList ignore = new ArrayList();

                Dictionary<String, Object> colisions = gameInfo.getObjects("colision");

                foreach (KeyValuePair<String, Object> i in colisions)
                {
                    Colision colision = (Colision)i.Value;
                    if(colision.Life > 0)
                        ignore.Add(colision.Id);
                }

                ignore.Add(projectile.IdEmissor);
                ignore.Add(projectile.Id);
                Object victim = gameInfo.checkColisionProjectile(projectile, type, ignore);
                if (victim != null)
                {
                    aiProjectile.Projectile.Movement = null;
                    projectile.IdVictim = victim.Id;
                    EventEmitter.getInstance().registerMessage(0, aiProjectile.Id, aiProjectile.Id, Message.MessageType.msg_destroy);
                }
                 */
                    /*
                else
                {
                    victim = gameInfo.checkColisionProjectile(projectile, ignore);
                    if (victim != null)
                    {
                        aiProjectile.Projectile.Movement = null;
                        projectile.IdVictim = victim.Id;
                        EventEmitter.getInstance().registerMessage(0, aiProjectile.Id, aiProjectile.Id, Message.MessageType.msg_destroy);
                    }
                }*/

                /*
                Object colision = gameInfo.checkColision(aiProjectile.Projectile);
                if (colision != null && colision.Id != projectile.IdEmissor)
                {

                    System.Console.WriteLine("colision detecteddd");

                    aiProjectile.Projectile.Movement = null;

                    EventEmitter.getInstance().registerMessage(0, aiProjectile.Id, aiProjectile.Id, Message.MessageType.msg_destroy);
                }
                */
            }
            //System.Console.WriteLine("projectilexitemovestate");
        }

        public override void exit(AIObject aiObject)
        {

        }

        public override Boolean onMessage(AIObject aiObject, Message message)
        {
    
            Boolean correct = false;
            switch (message.Type)
            {
                case Message.MessageType.msg_destroy:
                    {
                        aiObject.StateMachine.changeState(Projectile_DestroyState.getInstance());
                        correct = true;
                        break;
                    }
            }
            return correct;
        }

    }
}
